package com.ycy;

import com.ycy.juel.RDFAbpJuelForSkipTaskService;
import com.ycy.listener.YcyFlowableListener;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.engine.*;
import org.junit.Before;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class BaseTest {

  protected ProcessEngine processEngine;

  protected ProcessEngineConfiguration cfg;

  // 获取Flowable服务 Get Flowable repositoryService
  protected RepositoryService repositoryService;

  // 获取Flowable服务 Get Flowable runtimeService
  protected RuntimeService runtimeService;

  // 获取taskService对象 Get the first task
  protected TaskService taskService;

  protected HistoryService historyService;

  @Before
  public void before() {
    YCYStandaloneProcessEngineConfiguration ycyStandaloneProcessEngineConfiguration = new YCYStandaloneProcessEngineConfiguration();

//    ycyStandaloneProcessEngineConfiguration.initExpressFunction();

    cfg = ycyStandaloneProcessEngineConfiguration
            .setJdbcUrl("jdbc:mysql://127.0.0.1:3306/flowablebp?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8")
            .setJdbcUsername("root")
            .setJdbcPassword("123456")
            .setJdbcDriver("com.mysql.jdbc.Driver")
            // 如果数据表不存在的时候，自动创建数据表
            .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);

    //添加监听
    List<FlowableEventListener> eventListeners = new ArrayList<>();
    YcyFlowableListener e = new YcyFlowableListener();
    eventListeners.add(e);
//    eventListeners.add(new TransactionRollerBackFlowableListener());
//    eventListeners.add(new TransactionCommittingFlowableListener());
    this.cfg.setEventListeners(eventListeners);


    HashMap<Object, Object> map = new HashMap<>();
    map.put(RDFAbpJuelForSkipTaskService.class.getSimpleName(), new RDFAbpJuelForSkipTaskService());
    this.cfg.setBeans(map);


    // 执行完成后，就可以开始创建我们的流程了
    processEngine = cfg.buildProcessEngine();

    e.setProcessEngine(processEngine);

    // 获取Flowable服务 Get Flowable repositoryService
    repositoryService = processEngine.getRepositoryService();

    // 获取Flowable服务 Get Flowable runtimeService
    runtimeService = processEngine.getRuntimeService();

    // 获取taskService对象 Get the first task
    taskService = processEngine.getTaskService();

    historyService = processEngine.getHistoryService();

  }

}
